<?php 
class Session
{
    private static $_instance;
    
    protected static $_nameSpace = 'PrestaThemeEditor';
    
    protected $_session;
    
    private function __construct ()
    {
        if (!session_id())
            session_start();
        
        if (!isset($_SESSION[self::$_nameSpace]))
        {
            $_SESSION[self::$_nameSpace] = array();
        }
        $this->_session = $_SESSION[self::$_nameSpace];
    }
    
    public static function getInstance()
    {
        if (empty(self::$_instance))
        {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    
    public function __get($name)
    {
        if (isset($this->_session[$name]))
            return $this->_session[$name];
        elseif (isset($_SESSION[self::$_nameSpace][$name]))
        {
            $this->_session = $_SESSION[self::$_nameSpace];
            return $this->_session[$name];
        }
        else
            return NULL;
    }
    
    public function __set($name, $value)
    {
        $_SESSION[self::$_nameSpace][$name] = $value;
        $this->_session = $_SESSION[self::$_nameSpace];
    }
    
    public static function setNameSpace($nameSpace = 'PrestaThemeEditor')
    {
        self::$_nameSpace = $nameSpace;
    }
    
    public static function getNameSpace ()
    {
        return self::$_nameSpace;
    }
    
    public static function cleanNameSpace ($nameSpace)
    {
        if (isset($_SESSION[$nameSpace]))
            unset($_SESSION[$nameSpace]);
    }
}